import {getCartList} from 'api/api';
export default{
    state:{
        cartInfoList:[]
    },
    mutations:{
        getCartList(state,cartInfoList){
            state.cartInfoList=cartInfoList
        }
    },
    actions:{
        async getCartList(store){
           try{
                const {code,data:[info]}=await getCartList()
                let cartInfoList=[]
                if(code===200){
                    if(info){
                        cartInfoList=info.cartInfoList
                    }
                    store.commit('getCartList',cartInfoList)
                }
                return {code,info}
           }catch(e){
               return Promise.reject(e)
           }
        }
    },
    getters:{
        checkAll(state){
            let count=0;
            state.cartInfoList.forEach(item=>{
                if(item.isChecked===1){
                    count++
                }
            })
            return (count===state.cartInfoList.length)&&(state.cartInfoList.length!==0)
        },
        checkAllIds(state){
            let arr=[]
            state.cartInfoList.forEach(item=>{
                if(item.isChecked===1){
                    arr.push(item.skuId)
                }
            })
            return arr;
        },
        noCheckAllIds(state){
            let arr=[]
            state.cartInfoList.forEach(item=>{
                if(item.isChecked===0){
                    arr.push(item.skuId)
                }
            })
            return arr;
        },
        totalCheckedCount(state){
            let count=0;
            state.cartInfoList.forEach(item=>{
                if(item.isChecked===1){
                    count+=(+item.skuNum)
                }
            })
            return count
        },
        totalCheckedPrice(state){
            let price=0;
            state.cartInfoList.forEach(item=>{
                if(item.isChecked===1){
                    price+=(+item.skuPrice)*+(item.skuNum)
                }
            })
            return price
        }
    }
}